package de.uhlesoluschns.opensearch.provider;

import java.util.Locale;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import de.uhlesoluschns.opensearch.ui.SearchBox;

public class DatabaseHelper extends SQLiteOpenHelper {

	private static final String DB_NAME = "search.sqlite";
	private static final int DB_VERSION = 4;
	
	private SharedPreferences prefs;
	
	public DatabaseHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
		prefs = PreferenceManager.getDefaultSharedPreferences(context);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE \"engines\" (\"_id\" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , \"shortName\" TEXT NOT NULL , \"description\" TEXT NOT NULL , \"inputEncoding\" TEXT NOT NULL  DEFAULT '''UTF-8''', \"searchUrl\" TEXT NOT NULL , \"suggestionUrl\" TEXT, \"image\" BLOB, \"_order\" NUMERIC)");
		prefs.edit().putInt(SearchBox.PREF_PROVIDER, 1).commit();
		insertProviders(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		System.out.println("DatabaseHelper.onUpgrade()");
		System.out.println(oldVersion);
		System.out.println(newVersion);
		if(oldVersion >= 2) {
			db.execSQL("DELETE FROM engines WHERE _id IN (1,2)");
			if(oldVersion == 3) {
				db.execSQL("UPDATE engines SET _id = (SELECT MAX(_id) + 1 FROM engines) WHERE _id = 3");
			}
			insertProviders(db);
		} else {
			db.execSQL("DROP TABLE engines");
			onCreate(db);
		}
	}
	
	private void insertProviders(SQLiteDatabase db) {
		String lang = Locale.getDefault().getLanguage();
		if(TextUtils.isEmpty(lang)) {
			 lang = "en";
		}
		db.execSQL("INSERT INTO \"engines\" VALUES(1,'Google','Google Search','UTF-8','http://www.google.com/search?q=%s&ie=utf-8&oe=utf-8&hl=" + lang + "','http://suggestqueries.google.com/complete/search?output=firefox&hl=" + lang + "&q=%s',X'89504E470D0A1A0A0000000D49484452000000100000001008060000001FF3FF610000000473424954080808087C0864880000030A49444154388D25D2CB6B9C5518C0E1DF7BCEF74D2693C96D324913DAD868220D4A166D4421480356DB2EBC81525CA82D08EA565D765BB51B378A9BD60B6E6211514410830484DA7AC15B6C8DD11412D2A6D118D399746632DF9CCBEBA2FFC1B37800705EF5C7A5551D393EA3C9C31FE86B1FCF6B50AF59F5869E9F9ED2DF056D19A3013460B421E8162880F14ED55A38756E8195661F71D7DD9CFEE25F96D71BE43A3B997CFB2CA58E023646442C48042092B06250631308DE73F99F08C56EA2B56CEFA47CF8FD0A60C98FDF4536753F1E219A804768A945C5A3118C46482461574F1ED38A18328C582E2F7B108F58089D051C161B21518395404BC1194B22E616E9C844990B7FAC93B4F51263863109118BC960636191B521C38DCE5ED2160C546AF46F3B9C06922BAB15466F2BF1FCD13166CE5F65F1EF2A5DED9113D37B108433B3EF70EA4893AB77EEC74A8E1022ED0DC7FE2B3B3C7E7115D9F7ECFBFAEA89FB78627A9C4ADDF1D94F6B4C8D76333ADCCBE9D9B39CFCE12DEC60374425C40018C4096A52A46691FE63E7D465350E8C1478607290934F1F0451BE5EFA9943EF1E238E0D40342020AD1400751EF12978C1946C8B622ECFC5A5266FCCFCC5CA461D55CB1DA5DD8C8D1E80AC8989061312148F44454811404D0BE32A9BEC69CF78F49E32CF1C1DA7B21D5081E1BE212E3C7586C3E58368A3465401B544B935282AE05264E6AB5FF5A1C909FA7A0DBEBAC0D67F4B34B6AF3334F624F9623F2E781EFBE865BE5C9F43735D102CC64562B0104034A8D636E759FAF6457AF98D246DA099A1E6C6D97DE853BA4AA32C6E5CE3DEF78E73B3D0C27A8B06217A010F268B75FE9C7B81DB93EFD8DBDF64B0AB40B9A78DB22CB0F1CD4B28867DE5BD0C760C40E608A2C400A94F110F49ABBA41D15F239FF670B3E6116F89010AED2542730D7175764C07CD5A1D24C5F81C511C41031A15D3DE33425BFF83D4B73CDA2CA021472245C28EA56DE8113457E4F35F66B95E5D47824142841D436C458839923431E26A9B5A991F46AB9710D7C02579DAC60E939F788E4BABCBBCF2C99B84C462B2848080062085D7E7E47FC9907B91675A302C0000000049454E44AE426082',NULL);");
		db.execSQL("INSERT INTO \"engines\" VALUES(2,'Wapedia (" + lang + ")','Mobile Encyclopedia','UTF-8','http://wapedia.mobi/" + lang + "/Special:Search?skl=1&search=%s','http://api.wapedia.mobi/search/?method=opensearch_title&site=" + lang + "&search=%s',X'89504E470D0A1A0A0000000D49484452000000100000001008060000001FF3FF610000000473424954080808087C086488000001B349444154388DAD92BF6B535114C73FEFE6E5252F3598609786A2A48210A81D5A8C457073B0E0A4201517FF0237FF822EEE8E0ECE0E6EAEA2D5D4453B0A2679A082DA1F94469336C9BBEFDE7B1C5E7F20649087070E1CB8E7FBE1DC73BEDECED29AA86A892CE17A437C552D91AB553201005466E5FF02F8A22D32D299C4A22DBEDBFE8DF447D900438D2FD68171D900D6E14B6209EF2E50B87E09FD3E62F8FC03B9D92AE54737717B03FA6B2F0138FBF80E5E1870F0E41526DA4D0189459158E450535C99275CBD82C486A059A7B8324FE9FE325E1890AB55086F2F52B8D1C07CD9436283C406128B8F13E2B71D10C83766F04A0141B38E1B8C51E522C1D205D4B9290092CD6F487F7CFA0727283116FBBD47F2790B9422583C4FB03CC7E8C5266EFF907CB34E70750E8078BD8D68739AE6680244D0EB6DF28D19C2D526B95A05FDAE8B9A2E53B87611357D2605BC6EFFBD700F149202E2371D00C25B0B69732B42B7BAE42FCF92AB5590C118FDF12BC7FDC779E2C4B8D5458C05CFC344BBB89D3E7A2302EFE87D239A786EFFE4A60731FBF79EA2A60AD8AD5FE9D23EFDA4F7E0595A77B6277AC1FB51792820FFE29B4972FE00CF0ACB47E4DD70040000000049454E44AE426082',NULL);");
		db.execSQL("INSERT INTO \"engines\" VALUES(3,'Market','Search Android Market','UTF-8','market://search?q=%s',NULL,X'89504E470D0A1A0A0000000D49484452000000100000001008060000001FF3FF610000000473424954080808087C0864880000030F49444154388D75934D681C7518C67FEFCCEC976E76536B36DA16639A0A45C4DA50D4D6D8C45602D55630A2F562053D47C19BB7F4E04505AD878AA007351551E247A0D468AC1B7A8B2269B6C926DB2C69EC266D7737D9CDCE6466776667FE1EC420983EA7971FCFF3F0F2C22BDC46A74EBDF6B46D5BBD9562B16CD6EDE1C9C9C9D5AD7CC6BFC3E5CB998F37369C2722D1F0EF9E5BCF7CFFE3E873F3D9D981DC627E4F48B40F8157B62A10809999F9A30B8BF9F148284C22D1826559B8AE4747C77DB544323176EE8BE1EEED776FFBA8A7E7C8A570C32C3CD0DD5DDA2CF8636AEAA96BF9C58B2BCB2BB42493D46AEBEAFA5F4B18BAA1362C4B9E7F6140A2B1384280520A909B9EDBECEFED3D940110CFF38E08DAAF376EDDA0582C323D9D211A8928F490FC363EAE2CB3262FBE7C52058192B061A04488868DFDFDFDFD5300A2946A0DFCE04FD1A4534400B06D5B552A55B16A969A98484B6AE70EB55C58161121916831B7C5128F1D1F389EDD3C84E779A3AEEB2ADB7102DB719453AF07B6E328CBB303B3E928B36606E5D535353797535F9EFBAA7EF0E093A7811340520308083241A0104DD09546D48F105351A6DC1C347C744FC7F77D52ED6D2493C94848979DC0A3C0FD0680F2D592041A257D1D5377F8DA1EE3537314C7B578337192678D1E1ED476536DAC138FC749DD93BA03B001F79F0D82A0EA29975D5E9BFAC14973B636C28AB540C529F3796984438B2F91D7964884122A168DD1D1D9D50E34808A01A029CDF48226BE34F9C6FC191A1E8F870FD0AE25D921DB49C55BF9AEFA136DE124B9D20207BA1EDE0514FAFAFACA068088DC29A2A1A3F3FE5D6F70BE96A6542F53A9AF325ABBC0F2FA34380DDEDD77461E697D882BBF4C5F0366D3E974D300B89ABDBA5B8F1A7C2223DA9E6407DF96CF4B6163D6C7F3359AA25071410CF541EE8C0C473EABBE7EF6D5312007A0031CEB3B365F33D72606E74E674BA592BFCFEEBC77E6E66C044717DC40F03C2408C474D758C9166EE52F2CBC075CDFFC852DF576CB5EE2DA6134F730BADA8BE67589E1B7B69552178BEF148FDE368742868686B4FFF141BA788B671864FF7FF1DF7AE876536F1115780000000049454E44AE426082',NULL);");
	}
	
}
